﻿using MediatR;
using Traceability.App.Api.Application.Command.Basic;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model;

namespace Traceability.App.Api.Application.Handler.Basic
{
    public class DelBasicHandler : IRequestHandler<DelBasicCommand, ApiResult<int>>
    {
        IBaseRepository<BasicModel> basicRepo;

        public DelBasicHandler(IBaseRepository<BasicModel> basicRepo)
        {
            this.basicRepo = basicRepo;
        }

        public Task<ApiResult<int>> Handle(DelBasicCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var basic = basicRepo.GetAll().Where(c => c.Id == request.Id).FirstOrDefault();
            if (basic == null)
            {
                res.Msg = "您要删除的内容不存在";
                res.Code = ApiEnum.Fail;
                return Task.FromResult(res);
            }
            basic.IsDeleted = true;
            int a = basicRepo.Update(basic).Result;
            if (a > 0)
            {
                res.Msg = "申请信息删除成功";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "申请信息删除失败";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
